package ee.fj.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

public class Request {
	public static final String DEFAULT_PATH = "/";

	public static enum METHOD { GET, PUT, POST }

	private final BufferedReader in;
	public final METHOD method;
	private final Map<String, String> params;
	public final String path;
	private final String[] subPath;

	public Request(BufferedReader in, String method, String path, String[] subPath, Map<String, String> params) throws IOException {
		try {
			this.method = METHOD.valueOf(method);
		} catch (IllegalArgumentException e) {
			throw new IOException("Method " + method + " not supported!", e);
		}
		this.in = in;
		this.params = params;
		this.path = path;
		this.subPath = subPath;
	}
	public Request(BufferedReader in, METHOD method, String path, String[] subPath, Map<String, String> params) {
		this.method = method;
		this.in = in;
		this.params = params;
		this.path = path;
		this.subPath = subPath;
	}

	protected Request(Request request, String path, String[] subPath) {
		this(request.in, request.method, path, subPath, request.params);
	}

	public boolean isDefault() {
		return this.path.equals(DEFAULT_PATH);
	}
	public boolean hasParamater(String key) {
		return this.params.containsKey(key);
	}
	public String getParameter(String key) {
		if (hasParamater(key)) {
			return this.params.get(key);
		}
		return null;
	}
	public Integer getParameterAsInteger(String key) {
		String rv = getParameter(key);
		if (rv != null) {
			try {
				return Integer.parseInt(rv);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}
	public Float getParameterAsFloat(String key) {
		String rv = getParameter(key);
		if (rv != null) {
			try {
				return Float.parseFloat(rv);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}
	public final String[] getSubPath() {
		return this.subPath;
	}
	public String getSubPath(int i) {
		if (this.subPath.length > i) {
			return this.subPath[i];
		}
		return null;
	}
	public Integer getSubPathAsInteger(int i) {
		String rv = getSubPath(i);
		if (rv != null) {
			try {
				return Integer.parseInt(rv);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}
	public Float getSubPathAsFloat(int i) {
		String rv = getSubPath(i);
		if (rv != null) {
			try {
				return Float.parseFloat(rv);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}

	public Integer getPathAsInteger() {
		if (path != null) {
			try {
				return Integer.parseInt(path);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}
	public Float getPathAsFloat() {
		if (path != null) {
			try {
				return Float.parseFloat(path);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}

	public String readLine() throws IOException {
		return in.readLine();
	}
	public void close() throws IOException {
		this.in.close();
	}
}
